package com.hust.hui.quick.service.repository;

import com.hust.hui.quick.service.dao.TagMapper;
import com.hust.hui.quick.service.domain.TagEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Created by yihui on 2017/7/16.
 */
@Repository
public class TagRepository {

    @Autowired
    private TagMapper tagMapper;


    public void init() {
        tagMapper.create();
    }


    /**
     * 新增tag记录
     *
     * @param userId
     * @param tags
     * @return
     */
    public boolean addTags(long userId, List<String> tags) {
        List<TagEntity> list = queryByTags(userId, tags);
        Set<String> set = list.stream().map(TagEntity::getTag).collect(Collectors.toSet());

        List<TagEntity> entities = tags.stream().filter(tag -> !set.contains(tag)).map(
                tag -> {
                    TagEntity tagEntity = new TagEntity();
                    tagEntity.setUserId(userId);
                    tagEntity.setTag(tag);
                    tagEntity.setIsDeleted(0);
                    return tagEntity;
                }
        ).collect(Collectors.toList());


        return entities.size() <= 0 || tagMapper.insert(entities) == entities.size();

    }


    public List<TagEntity> queryByTags(long userId, List<String> tags) {
        Map<String, Object> map = new HashMap<>();
        map.put("tags", tags);
        map.put("userId", userId);


        return query(map);
    }


    public TagEntity queryByTag(long userId, String tag) {
        Map<String, Object> map = new HashMap<>();
        map.put("tag", tag);
        map.put("userId", userId);


        List<TagEntity> list = query(map);
        return list.size() > 0 ? list.get(0) : null;
    }


    public TagEntity queryById(long tagId) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", tagId);

        List<TagEntity> list = query(map);
        return list.size() > 0 ? list.get(0) : null;
    }


    public List<TagEntity> queryByIds(List<Long> tagIds) {
        Map<String, Object> map = new HashMap<>();
        map.put("ids", tagIds);

        return query(map);
    }

    private List<TagEntity> query(Map<String, Object> params) {
        params.put("isDeleted", 0);

        return tagMapper.query(params);
    }


    /**
     * 删除标签
     *
     * @param id
     * @return
     */
    public boolean delete(long id) {
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        params.put("isDeleted", 1);
        return tagMapper.update(params) > 0;
    }


    /**
     * 更新标签名
     *
     * @param id
     * @param tag
     * @return
     */
    public boolean updateTagName(long id, String tag) {
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        params.put("tag", tag);
        return tagMapper.update(params) > 0;
    }
}
